<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 刷题分类
 *
 * @icon fa fa-circle-o
 */
class Questioncate extends Backend
{

    /**
     * Questioncate模型对象
     * @var \app\admin\model\Questioncate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Questioncate;
        $this->tree = Tree::instance();
        $this->tree->init(\app\admin\model\Questioncate::allQuestionmenu(), 'pid');
        $this->questioncateList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
        
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isDetailsList", $this->model->getIsDetailsList());
    }



    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $search = $this->request->request("search");
            $type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];

            foreach ($this->questioncateList as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $this->questioncateList;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->questioncateList;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
                }
            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function selectpage()
    {
        return parent::selectpage();
    }

}
